/*
 * Producer.java
 *
 * Created on October 20, 2007, 6:52 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package simulationengine.process;

//import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.TimeUnit;
import simulationengine.animation.AnimationCommand;

/**
 *
 * @author jkeister
 */
public class Producer extends Thread {
    
    int time = 0;
    //int threadCount = 0;
    final int endTime;
    final Resource r;
    final Distribution dist;
    
    volatile int counter = 0;
    
    Consumer conThread;
    
    final boolean DEBUG = false;
    
    //LinkedBlockingQueue<AnimationCommand> commands;
    PriorityBlockingQueue<AnimationCommand> commands;
    
    
    //final boolean debug;
    //final boolean queueCommands;
    
    /** Creates a new instance of Producer */
    public Producer(int end, Distribution distro, Resource resource,  PriorityBlockingQueue<AnimationCommand> commands) 
    {
        endTime = end;
        r = resource;
        dist = distro;
        //debug = DEBUG;
	   
	   this.commands = commands;
    }
    
    public void run()
    {
        System.out.println("Starting producer");
        while(time < endTime)
        {
                if(DEBUG)
                    System.out.println("Started new thread at: " + time);
                conThread = new Consumer(endTime,time,r,counter++, commands);
                
                //commands.add(new AnimationCommand(AnimationCommand.Command.ADD, time, counter, 0, 0));
                
                conThread.start();
                time = dist.next();
                                
                yield();
        }
        System.out.println("Done producing");
    }
}